<template>
  <a-modal
    title="确认支付"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="支付方式"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            placeholder="请选择支付方式"
            @select="onSelectPay"
            v-decorator="[
              'Payitem',
              {rules: [{ required: true, message: '请选择支付方式'}]}
            ]" >
            <a-select-option :key="index" v-for="(item, index) in payType">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="优惠方式"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select
            placeholder="请选择优惠方式"
            @select="onSelectDis"
            v-decorator="[
              'cmDiscount',
              {rules: [{ required: false, message: '请选择类别'}]}
            ]" >
            <a-select-option :key="index" v-for="(item, index) in payYh">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="支付金额"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          {{price}}
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { GetFeeOrerBase , submitOrders} from '@/api/manage'
import { mapGetters } from 'vuex'
export default {
  props: ['feeInfo','housesInfo'],
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      },
      visible: false,
      confirmLoading: false,

      form: this.$form.createForm(this),
      price: 0,
      cmDiscountType:0,
      cmDiscountType:0,
      PayitemId:'',
      normalPrice:0,
      payType:[],
      payYh:[]
    }
  },
  methods: {
    ...mapGetters(['token', 'pmsArId', 'nowMg']),
    pay () {
       this.visible = true
       this.getBsasedata()
       let money=0
       console.log(this.feeInfo)
       this.feeInfo.forEach((value,index)=>{
           money+=parseInt(value.price) 
       })
       this.price=money
       this.normalPrice=money
    },
    getBsasedata (){
        GetFeeOrerBase({token: this.token(), ar_id: this.pmsArId(), cm_mg_id: this.nowMg()}).then(res=>{
            this.payType=res.payItem
            this.payYh=res.cmDiscount
        })
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
           submitOrders(
               {token: this.token(), 
               ar_id: this.pmsArId(), 
               cm_mg_id: this.nowMg(),
               bd_message_id:this.housesInfo.bd_message_id,
               period_message_id:this.housesInfo.period_message_id,
               unit_message_id:this.housesInfo.unit_message_id,
               houses_id:this.housesInfo.id,
               moeny:this.price,
               dicount_type:this.cmDiscountType,
               dicount_content:this.cmDiscountContent,
               pay_type:this.PayitemId,
               ext_order:this.feeInfo
               }).then(res=>{
                    this.confirmLoading = false;
                   if (res.code == 0) {
                        this.visible = false
                        this.$emit('ok')
                    } else {
                        this.$notification['error']({
                        message: '错误',
                        description: res.msg,
                        duration: 4
                        })
                    }
               })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.visible = false
    },
    onSelectDis(value){
       this.cmDiscountType=this.payYh[value].type
       this.cmDiscountContent=this.payYh[value].content
       if(this.payYh[value].type==1){
           this.price=this.price-this.payYh[value].content
       }else{
           this.price=this.normalPrice
       }
    },
    onSelectPay(value){
        this.PayitemId=this.payType[value].id;
    }
  }
}
</script>
